<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品营养成分信息
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 * @version 1.0
 */

class goodsNutrition {

    private $dbca;

    public function __construct( $dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 查询商品营养成分信息
     * create by allen.qiang
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchBasicGroup( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //基本信息组ID
        if (isset($arg_array['basicGroupID']) && is_numeric($arg_array['basicGroupID'])) {
            $sql .= ' AND basicGroupID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['basicGroupID'];
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg']) && in_array($arg_array['useFlg'],array(0,1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //扩展
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_basic_group ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname'])){
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);
                }


                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_basic_group ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'basicGroupID' => $item->basicGroupID,
                    'chinese' => $item->chinese,
                    'french' => $item->french,
                    'sortOrder' => $item->sortOrder,
                    'useFlg' => $item->useFlg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询商品基本信息值
     * create by allen.qiang
     * @param $arg_array 参数数组
     * @return array|null
     */
    public function searchBasic( &$arg_array ) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //基本信息组ID
        if (isset($arg_array['basicGroupID']) && is_numeric($arg_array['basicGroupID'])) {
            $sql .= ' AND basicGroupID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['basicGroupID'];
        }

        //基本信息值ID
        if (isset($arg_array['basicID']) && is_numeric($arg_array['basicID'])) {
            $sql .= ' AND basicID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['basicID'];
        }

        //基本信息值
        if (isset($arg_array['basicName']) && is_numeric($arg_array['basicName'])) {
            $sql .= ' AND basicName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . $arg_array['basicName'] . '%';
        }

        //使用标志
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //扩展
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM goods_basic ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM goods_basic ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {

                $item_array[] = array(
                    'basicGroupID' => $item->basicGroupID,
                    'chinese' => $item->chinese,
                    'french' => $item->french,
                    'basicID' => $item->basicID,
                    'basicName' => $item->basicName,
                    'sortOrder' => $item->sortOrder,
                    'useFlg' => $item->useFlg
                );

            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * goods_nutrition查询
     */
    public function searchNutrition(&$arg_array) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //营养成分信息ID
        if (isset($arg_array['nid']) && is_numeric($arg_array['nid'])) {
            $sql .= ' AND nid = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['nid'];
        }

        //营养成分信息组ID
        if (isset($arg_array['nGroupID']) && is_numeric($arg_array['nGroupID'])) {
            $sql .= ' AND nGroupID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['nGroupID'];
        }

        //营养成分值
        if (isset($arg_array['name']) && !empty($arg_array['name'])) {
            $sql .= ' AND name LIKE ?';
            $params_type .= 's';
            $params_array[] = $arg_array['name'];
        }

        //营养成分百分比
        if (isset($arg_array['npct']) && trim($arg_array['npct']) != '') {
            $sql .= ' AND npct LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['npct']) . '%';
        }

        //营养成分信息组名称
        if (isset($arg_array['nGroupName']) && trim($arg_array['nGroupName']) != '') {
            $sql .= ' AND nGroupName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['nGroupName']) . '%';
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg']) && in_array($arg_array['useFlg'],array(0,1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }
        
         //扩展
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计行数
                $search_sql  = 'SELECT COUNT(*) AS count FROM goods_nutrition ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count  = $count_array[0]->count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY goods_basic.%s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询

            $search_sql = 'SELECT * FROM goods_nutrition' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            if ($temp_item_array) {
                foreach($temp_item_array as &$item) {
                    $item_array[] = array(
                        'nid' 	        => $item->nid,
                        'name'  	    => $item->name,
                        'npct'  	    => $item->npct,
                        'nGroupID' 	=> $item->nGroupID,
                        'nGroupName'   => $item->nGroupName,
                        'sortOrder'    => $item->sortOrder,
                        'useFlg' 	    => $item->useFlg
                    );
                }
            }

            return isset($item_array) ? $item_array : null;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    /**
     * 商品营养成分信息查询
     */
    public function searchgoodsNutrition(&$arg_array) {
        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //营养成分信息组ID
        if (isset($arg_array['nGroupID']) && is_numeric($arg_array['nGroupID'])) {
            $sql .= ' AND nGroupID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['nGroupID'];
        }

        //营养成分信息组名称
        if (isset($arg_array['nGroupName']) && trim($arg_array['nGroupName']) != '') {
            $sql .= ' AND nGroupName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['nGroupName']) . '%';
        }

        //排序序号
        if (isset($arg_array['sortOrder']) && is_numeric($arg_array['sortOrder'])) {
            $sql .= ' AND sortOrder = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['sortOrder'];
        }

        //使用标志
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg']) && in_array($arg_array['useFlg'],array(0,1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }
        
        //扩展
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计促销行数
                $search_sql  = 'SELECT COUNT(*) AS count FROM goods_nutrition_group ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count  = $count_array[0]->count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询商品促销信息

            $search_sql = 'SELECT nGroupID,nGroupName,sortOrder,useFlg FROM goods_nutrition_group' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            if ($temp_item_array) {
                foreach($temp_item_array as &$item) {

                    //根据基本信息组ID，查找基本信息
                    $goods_basic_array = array(
                        'nGroupID' 	=> $item->nGroupID
                    );

                    $goods_nutrition = $this->searchNutrition($goods_basic_array);

                    $item_array[] = array(
                        'nGroupID' 	     => $item->nGroupID,
                        'nGroupName' 	     => $item->nGroupName,
                        'sortOrder'         => $item->sortOrder,
                        'useFlg' 	         => $item->useFlg,
                        'goods_basic'      => $goods_nutrition
                    );
                }
            }

            return isset($item_array) ? $item_array : null;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    /**
     * 获取基本信息组ID的序号
     */
    public function searchSort() {

        $maxID = $this->dbca->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,1,MAX(sortOrder)+1) as maxID FROM goods_nutrition_group');

        $maxID = $maxID[0]->maxID + 1;

        return $maxID;
    }

    /**
     * 商品营养的保存/修改
     */
    public function savegoodsBasic($arg_array) {
        global $G_PGLOB;
        /**
         * 检查参数
         */
        //定义错误变量
        $error_msg = '';

        if (isset($arg_array['nGroupID']) && !empty($arg_array['nGroupID'])) {
            $nGroupID = $arg_array['nGroupID'];
        } else {
            $nGroupID = 0;
        }

        if (isset($arg_array['nGroupName']) && !empty($arg_array['nGroupName'])) {
            $nGroupName = $arg_array['nGroupName'];
        } else {
            $error_msg .= '营养成分信息组名称<br />';
        }

        if (isset($arg_array['sortOrder']) && is_numeric($arg_array['sortOrder'])) {
            $sortOrder = $arg_array['sortOrder'];
        } else {
            $error_msg .= '请输入正确的排序序号<br />';
        }

        if (isset($arg_array['basic_array']) && is_array($arg_array['basic_array'])) {
            $basic_array = $arg_array['basic_array'];
        } else {
            $error_msg .= '请输入营养成分值<br />';
        }

		$percent_array = isset($arg_array['percent_array']) ? $arg_array['percent_array'] : '';

        if (isset($arg_array['use_array']) && is_array($arg_array['use_array'])) {
            $use_array = $arg_array['use_array'];
        } else {
            $error_msg .= '使用标志不正确<br />';
        }


        //判断数组中有没有重复的值
        if (count($basic_array) != count(array_unique($basic_array))) {
            $error_msg .= '商品基本信息值存在重复<br />';
        }


        //判断序号不能重复
        if ($arg_array['nGroupID'] == '') {
            $allsortOrder = $this->dbca->stmt_query('SELECT sortOrder FROM goods_nutrition_group');
            foreach ($allsortOrder as $v) {
                if ($v->sortOrder == $sortOrder) {
                    $error_msg .= '排序序号已存在<br />';
                }
            }
        } else {
            $allsortOrder = $this->dbca->stmt_query('SELECT sortOrder FROM goods_nutrition_group WHERE nGroupID != ?','i',$arg_array['nGroupID']);
            foreach ($allsortOrder as $v) {
                if ($v->sortOrder == $sortOrder) {
                    $error_msg .= '排序序号已存在<br />';
                }
            }
        }

        if ($error_msg != '') {
            return array( 'code' => 400 , 'message' => $error_msg );
        }

        /**
         * 数据存入数据库
         */
        try{
            $this->dbca->beginTransaction();

            //存基本信息组
            $goods_basic_group = new entity('goods_nutrition_group');

            if ($nGroupID > 0) {
                $goods_basic_group->nGroupID = $arg_array['nGroupID'];
                if (!$this->dbca->fetch($goods_basic_group)) {
                    return array( 'code' => 400 , 'message' => '营养成分信息ID不存在' );
                }
                $goods_basic_group->editor = $G_PGLOB->currentUserID;
            } else {
                $goods_basic_group->creator = $G_PGLOB->currentUserID;
                $goods_basic_group->useFlg  = 1;
            }
            $goods_basic_group->nGroupName      = $arg_array['nGroupName'];
            $goods_basic_group->sortOrder       = $arg_array['sortOrder'];

            if (!$this->dbca->save($goods_basic_group)) {
                throw new Exception('商品营养成分信息保存失败',400);
            }

            $flg = 0;   //添加的标志
            if ($nGroupID == 0) {
                $nGroupID = $this->dbca->insert_id;
            } else {
                $nameInfo = $this->dbca->stmt_query('SELECT name FROM goods_nutrition WHERE nGroupID = ?','i',$nGroupID);
                $flg = 1;   //修改的标志
            }

            /**
             * 修改基本信息
             */
            if (isset($nameInfo) && !empty($nameInfo)) {
                foreach($nameInfo as $v) {
                    $name = $v->name;
                    if (in_array($name,$basic_array)) {
                        $goods_nutrition = new entity('goods_nutrition');
                        $goods_nutrition->nGroupID = $nGroupID;
                        $goods_nutrition->name = $name;
                        if (!$this->dbca->fetch($goods_nutrition)) {
                            throw new Exception ('没有此基本信息' , 404 );
                        }
                        $k = array_search($name,$basic_array);
                        $goods_nutrition->nGroupName        = $nGroupName;
                        $goods_nutrition->useFlg            = $use_array[$k];
                        $goods_nutrition->npct              = isset($percent_array[$k]) ? $percent_array[$k] : '';

                        $this->dbca->save($goods_nutrition);

                        //将当前基本信息删除
                        unset($basic_array[$k]);
                        unset($use_array[$k]);

                    } else {
                        $this->dbca->stmt_query('DELETE FROM goods_nutrition WHERE nGroupID = ? AND name = ?','is',array($nGroupID,$name));
                    }
                }
            }

            /**
             * 添加基本信息
             */
            //获取商品基本信息的序号
            $basic_order = $this->dbca->stmt_query('SELECT IF(MAX(sortOrder) IS NULL ,1,MAX(sortOrder)+1) AS basic_order FROM goods_nutrition');
            $basic_order = $basic_order[0]->basic_order;

            //保存基本信息值
            foreach($basic_array as $k=>$v) {
                foreach($use_array as $k1=>$v1) {
                    if ($k == $k1) {
                        $goods_nutrition = new entity('goods_nutrition');
                        $goods_nutrition->creator       = $G_PGLOB->currentUserID;
                        $goods_nutrition->name          = $v;
                        $goods_nutrition->npct          = isset($percent_array[$k]) ? $percent_array[$k] : '';
                        $goods_nutrition->useFlg        = $v1;
                        $goods_nutrition->nGroupID      = $nGroupID;
                        $goods_nutrition->nGroupName    = $arg_array['nGroupName'];
                        $goods_nutrition->sortOrder     = $basic_order;

                        if (!$this->dbca->save($goods_nutrition)) {
                            throw new Exception('基本信息保存失败',400);
                        }

                        $basic_order++;
                    }
                }
            }

            $this->dbca->commit();

            if(!$flg){
                return array( 'code' => 200 , 'message' => '数据保存成功','nGroupID' => 0,'sortOrder' => $arg_array['sortOrder']+1);
            }else{
                return array( 'code' => 200 , 'message' => '数据修改成功','nGroupID' => 1);
            }


        } catch(Exception $e) {

            $this->dbca->rollback();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode() , 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500 , 'message' => '系统错误' );
            }

        }

    }

    /**
     * 商品营养的删除
     */
    public function delgoodsBasic($basicGroupID) {

        try{

            $this->dbca->beginTransaction();
            $j = count($basicGroupID);
            for($i=0;$i<$j;$i++){
                $goods_basic_group = new entity('goods_nutrition_group');
                $goods_basic_group->nGroupID = $basicGroupID[$i];
                if (!$this->dbca->fetch($goods_basic_group)) {
                    throw new Exception('要删除的营养成分信息不存在',404);
                } else {
                    if (!$this->dbca->delete($goods_basic_group)) {
                        throw new Exception('删除营养成分信息失败',400);
                    }
                }

                if (!$this->dbca->stmt_query('DELETE FROM goods_nutrition WHERE nGroupID = ?','i',$basicGroupID[$i])) {
                    throw new Exception('删除基本信息值失败',400);
                }
            }

            $this->dbca->commit();

            return array( 'code' => 200 , 'message' => 'ok' );

        } catch (Exception $e) {

            $this->dbca->rollback();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode() , 'message' => $e->getMessage() );
            } else {
                return array( 'code' => 500 , 'message' => '系统错误' );
            }

        }

    }

    /**
     * 商品营养的启用和禁用
     */
    public function changeUseFlg($nGroupID,$useFlg) {

        try{


            //检测参数
            $goods_basic_group = new entity('goods_nutrition_group');
            $goods_basic_group->nGroupID = $nGroupID;
            if (!$this->dbca->fetch($goods_basic_group)) {
                throw new Exception( '查无此商品营养成分信息' , 404 );
            }

            if (!is_numeric($useFlg) || !in_array($useFlg,array(0,1))) {
                throw new Exception( '使用状态不正确' , 404 );
            }

            //多页面操作问题
            if ($useFlg == 1 && $goods_basic_group->useFlg == 1) {
                throw new Exception( '营养成分信息已经启用,请刷新页面' , 400 );
            }

            if ($useFlg == 0 && $goods_basic_group->useFlg == 0) {
                throw new Exception( '营养成分信息已经禁用,请刷新页面' , 400 );
            }

            //使用状态修改
            $goods_basic_group->useFlg = $useFlg;

            if (!$this->dbca->save($goods_basic_group)) {
                throw new Exception( '使用状态修改失败' , 400 );
            }

            return array( 'code' => 200 , 'message' => 'ok' );

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode() , 'message' => $e->getMessage() );
            } else {
                return array( 'code' => 500 , 'message' => '系统错误' );
            }

        }
    }
    
    /**
	 * 批量添加营养成分信息
	 *
	 * @param unknown_type $arg_array
	 */
	public function searchNutri( &$arg_array ){
		
		//检验参数
    	if (!isset($arg_array['params']) || empty($arg_array['params'])) {
    		return array('code' => 400, 'message' => '参数错误');
    	}
    	
    	$group_str = '';
    	$natri_array = array(
    		'useFlg' => 1
    	);

    	foreach ($arg_array['params'] as $p) {
    		$group_str .= $p . ',';
    	}
    	if (!empty($group_str)) {
			$group_str = substr($group_str, 0, -1);
			$natri_array['extend'] = " AND nGroupID IN ($group_str) ORDER BY nGroupID ASC,sortOrder ASC";
    	} else {
    		return array('code' => 400, 'message' => '营养成分信息未找到');
    	}
    	
    	$nutri_result = $this->searchNutrition( $natri_array );
    	if (!$nutri_result) {
    		return array('code' => 400, 'message' => '营养成分信息未找到');
    	}

    	return array('code' => 200, 'data' => $nutri_result);

    }
    
    /**
     * 查询商品营养成分信息映射
     */
    public function searchGoodsNutriRelation(&$arg_array) {
    	//sql字符串
    	$sql = '';
    	
    	//参数类型字符串
    	$params_type = '';
    	
    	//参数值数组
    	$params_array = array();
    	
    	//商品网购号
    	if (isset($arg_array['postID']) && is_numeric($arg_array['postID'])) {
    		$sql .= ' AND postID = ?';
    		$params_type .= 'i';
    		$params_array[] = $arg_array['postID'];
    	}
    
    	//扩展
    	if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
    		$sql .= $arg_array['extend'];
    	}
    	
    	if ($sql) {
    		$sql = ' WHERE ' . substr($sql, 4);
    	}
    	
    	try {
    	
    		if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {
    	
    			//统计促销行数
    			$search_sql  = 'SELECT COUNT(*) AS count FROM goods_nutrition_relation ' . $sql;
    			$count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    			$item_count  = $count_array[0]->count;
    			if ($item_count == 0) {
    				$arg_array['count'] = 0;
    				return null;
    			}
    	
    			$arg_array['count'] = $item_count;
    	
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY goods_produce.%s %s', $arg_array['sortname'], $arg_array['sortorder']);
    	
    			//分页
    			if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
    				$arg_array['page'] = 1;
    	
    			$sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
    	
    		} else {
    	
    			$arg_array['count'] = 0;
    	
    			//排序
    			if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
    				$sql .= sprintf(' ORDER BY goods_produce.%s %s', $arg_array['sortname'], $arg_array['sortorder']);
    	
    		}
    	
    		//查询商品促销信息
    	
    		$search_sql = 'SELECT * FROM goods_nutrition_relation' . $sql;
    		$temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
    	
    		if ($temp_item_array) {
    			foreach($temp_item_array as &$item) {
    				$item_array[] = array(
						'postID'	=> $item->postID,
						'nGroupID'	=> $item->nGroupID,
						'nGroupName'	=> $item->nGroupName,
						'nid'	=> $item->nid,
						'nName'	=> $item->name,
						'npct'	=> $item->npct
    				);
    			}
    		}
    	
    		return isset($item_array) ? $item_array : null;
    	
    	} catch (Exception $e) {
    	
    		error_log($e->getMessage());
    		return null;
    	
    	}
    }

}